#!/bin/bash
COMMAND=0
SCRIPT=$0
INSTALLPATH=/usr/local/advtty
CONFIGFILE=$INSTALLPATH/advttyd.conf
#!/bin/bash
# Argument = -t test -r server -p password -v


usage()
{
cat << EOF
usage: $0 options

This script adds serial server device to local virtual serial port service. 

OPTIONS:
   -h      Show this message
   -t      Set type of the device server
   -a      Set address of the device server
   -p      Set port willing to mount on the device server(>=1)
   -m      Minor number(0-255)
   -r      Redundant IP address of the device server (Optional)

Example:
   $0 -t 1524 -a 10.0.0.1 -p 1 -m 1
   $0 -t 1522 -a 192.168.1.100 -p 2 -m 0 -r 192.168.1.101
   
EOF
}
MINOR=
ADDRESS=
PORT=
TYPE=
RADDRESS=
cleanUp()
{
sed '/^$/d' $CONFIGFILE > $INSTALLPTATH/advttyd.tmp
sort -g $INSTALLPTATH/advttyd.tmp > $INSTALLPTATH/advttyd.conf	
rm $INSTALLPTATH/advttyd.tmp -f
}

addDevice()
{
COUNT=`egrep "$MINOR[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+" $CONFIGFILE -c`

if [ $COUNT -gt 0 ]
then
	echo "Local serial port advtty$MINOR changed to Device $TYPE at $ADDRESS"
	`sed -i -r s/"$MINOR[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+.*"/"$MINOR   $TYPE    $ADDRESS    $PORT     $RADDRESS"/g $CONFIGFILE`
else
	`echo "$MINOR   $TYPE    $ADDRESS    $PORT     $RADDRESS" >> $CONFIGFILE`
	echo "Device $TYPE at $ADDRESS added to local serial port advtty$MINOR"
fi
cleanUp
	echo 'You should now reset the local deamon by using "advman"'
}

while getopts “ht:a:p:m:r:” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         t)
             TYPE=$OPTARG
             ;;
         a)
             ADDRESS=$OPTARG
             ;;
         p)
             PORT=$OPTARG
             ;;
         m)
             MINOR=$OPTARG
             ;;
         r)
         	 RADDRESS=$OPTARG
         	 ;;
         ?)
	     usage
             exit
             ;;
     esac
done

if [[ -z $TYPE ]] || [[ -z $ADDRESS ]] || [[ -z $PORT ]] || [[ -z $MINOR ]]
then
     usage
     exit 1
else
	# Guider:: check for valid IPv4 & IPv6
	#if [ `echo $ADDRESS|egrep "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" -c` -lt 1 ]
	if [ `echo $ADDRESS|egrep "(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\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